package es.udc.emarketapp.emarket.model.orderDetails.dao;

import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;

import es.udc.emarketapp.emarket.model.orderDetails.vo.OrderDetailsVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;

public abstract class AbstractSQLOrderDetailsDAO implements SQLOrderDetailsDAO {

    protected AbstractSQLOrderDetailsDAO() {
    }

    public boolean exists(Connection connection, Long nPedido, Long nCompra)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido FROM pedido "
                    + "WHERE n_pedido = ? AND n_compra = ?";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setLong(1, nPedido.longValue());
            preparedStatement.setLong(2, nCompra.longValue());

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            return resultSet.next();

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// exists
        /*
    public OrderDetailsVO findById(Connection connection, Long nPedido,
    Long nCompra) throws InstanceNotFoundException,
    InternalErrorException {

    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    try {

    // Create "preparedStatement".
    String queryString = "SELECT id_vendedor, valor, "
    + "peso, gastos_envio, fecha_pago, fecha_envio "
    + "FROM pedido WHERE n_pedido = ? " + "AND n_compra = ? ";
    preparedStatement = connection.prepareStatement(queryString);

    // Fill "preparedStatement".
    preparedStatement.setLong(1, nPedido.longValue());
    preparedStatement.setLong(2, nCompra.longValue());

    // Execute query.
    resultSet = preparedStatement.executeQuery();

    if (!resultSet.next()) {
    throw new InstanceNotFoundException(nPedido,
    OrderDetailsVO.class.getName());
    }

    // Get results.
    int i = 1;
    String idVendedor = resultSet.getString(i++);
    double valor = resultSet.getDouble(i++);
    double peso = resultSet.getDouble(i++);
    double gastosEnvio = resultSet.getDouble(i++);
    Calendar fechaPago = null;
    Timestamp fPago = resultSet.getTimestamp(i++);
    if (fPago != null) {
    fechaPago = Calendar.getInstance();
    fechaPago.setTimeInMillis(fPago.getTime());
    }
    Calendar fechaEnvio = null;
    Timestamp fEnvio = resultSet.getTimestamp(i++);
    if (fEnvio != null) {
    fechaEnvio = Calendar.getInstance();
    fechaEnvio.setTimeInMillis(fEnvio.getTime());
    }

    /*
     * Calendar fechaPago = Calendar.getInstance();
     * fechaPago.setTimeInMillis
     * ((resultSet.getTimestamp(i++)).getTime());
     *
     * Calendar fechaEnvio = Calendar.getInstance();
     * fechaEnvio.setTimeInMillis
     * ((resultSet.getTimestamp(i++)).getTime());
     */
    /*
    // Return the value object.
    return new OrderDetailsVO(nPedido, nCompra, idVendedor, valor,
    peso, gastosEnvio, fechaPago, fechaEnvio);

    } catch (SQLException e) {
    throw new InternalErrorException(e);
    } finally {
    GeneralOperations.closeResultSet(resultSet);
    GeneralOperations.closeStatement(preparedStatement);
    }

    }// findById
     */

    public OrderDetailsVO findById(Connection connection, Long nPedido)
            throws InstanceNotFoundException, InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT id_vendedor, n_compra, valor, "
                    + "peso, gastos_envio, fecha_pago, fecha_envio "
                    + "FROM pedido WHERE n_pedido = ? ";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setLong(1, nPedido.longValue());

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            if (!resultSet.next()) {
                throw new InstanceNotFoundException(nPedido,
                        OrderDetailsVO.class.getName());
            }

            // Get results.
            int i = 1;
            String idVendedor = resultSet.getString(i++);
            long nCompra = resultSet.getLong(i++);
            double valor = resultSet.getDouble(i++);
            double peso = resultSet.getDouble(i++);
            double gastosEnvio = resultSet.getDouble(i++);
            Calendar fechaPago = null;
            Timestamp fPago = resultSet.getTimestamp(i++);
            if (fPago != null) {
                fechaPago = Calendar.getInstance();
                fechaPago.setTimeInMillis(fPago.getTime());
            }
            Calendar fechaEnvio = null;
            Timestamp fEnvio = resultSet.getTimestamp(i++);
            if (fEnvio != null) {
                fechaEnvio = Calendar.getInstance();
                fechaEnvio.setTimeInMillis(fEnvio.getTime());
            }

            /*
             * Calendar fechaPago = Calendar.getInstance();
             * fechaPago.setTimeInMillis
             * ((resultSet.getTimestamp(i++)).getTime());
             *
             * Calendar fechaEnvio = Calendar.getInstance();
             * fechaEnvio.setTimeInMillis
             * ((resultSet.getTimestamp(i++)).getTime());
             */

            // Return the value object.
            return new OrderDetailsVO(nPedido, nCompra, idVendedor, valor,
                    peso, gastosEnvio, fechaPago, fechaEnvio);

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findById

    public List<OrderDetailsVO> findByPurchase(Connection connection,
            Long nCompra, int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, id_vendedor, "
                    + "valor, peso, gastos_envio, fecha_pago, "
                    + "fecha_envio FROM pedido WHERE " + "n_compra = ? ";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            preparedStatement.setLong(1, nCompra);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    String idVendedor = resultSet.getString(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByPurchase

    public List<OrderDetailsVO> findBySeller(Connection connection,
            String idVendedor, int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, "
                    + "valor, peso, gastos_envio, fecha_pago, "
                    + "fecha_envio FROM pedido WHERE "
                    + "id_vendedor = ? ORDER BY n_compra DESC";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            preparedStatement.setString(1, idVendedor);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByseller


     public List<OrderDetailsVO> findByDate(Connection connection,
            Calendar startDate, Calendar endDate, int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, "
                    + "id_vendedor, valor, peso, gastos_envio, "
                    + "fecha_pago, fecha_envio FROM pedido WHERE "
                    + " n_compra IN (SELECT n_compra from compra "
                    + "WHERE fecha >= ? AND fecha <= ?)";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
         /*
            Calendar startDateWithoutMilliSeconds = 
                DateOperations.getDateWithoutMilliSeconds(startDate);
            Calendar endDateWithoutMilliSeconds = 
                DateOperations.getDateWithoutMilliSeconds(endDate);
            preparedStatement.setTimestamp(i++, new Timestamp(
                startDateWithoutMilliSeconds.getTime().getTime()));
            preparedStatement.setTimestamp(i++, new Timestamp(
                endDateWithoutMilliSeconds.getTime().getTime()));
          */
            
          preparedStatement.setTimestamp(1,
                    new Timestamp(startDate.getTimeInMillis()));
          preparedStatement.setTimestamp(2,
                    new Timestamp(endDate.getTimeInMillis()));
            
                      
            
            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    String idVendedor = resultSet.getString(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByDate

    public List<OrderDetailsVO> findByPaidPendent(Connection connection,
            int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, id_vendedor, "
                    + "valor, peso, gastos_envio, fecha_pago, "
                    + "fecha_envio FROM pedido WHERE fecha_pago IS NULL "
                    + "ORDER BY id_vendedor, n_compra ASC";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);


            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    String idVendedor = resultSet.getString(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByPaidPendent

    public List<OrderDetailsVO> findByPaidPendentAndSeller(Connection connection,
            String idVendedor, int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, "
                    + "valor, peso, gastos_envio, fecha_pago, "
                    + "fecha_envio FROM pedido WHERE "
                    + "id_vendedor = ? AND fecha_pago IS NULL "
                    + "ORDER BY n_compra ASC";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            preparedStatement.setString(1, idVendedor);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByPaidPendentAndseller

    public List<OrderDetailsVO> findByShippingPendent(Connection connection,
            int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, id_vendedor, "
                    + "valor, peso, gastos_envio, fecha_pago, "
                    + "fecha_envio FROM pedido WHERE fecha_envio IS NULL "
                    + "ORDER BY id_vendedor, n_compra ASC";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);


            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    String idVendedor = resultSet.getString(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByShippingPendent

    public List<OrderDetailsVO> findByShippingPendentAndSeller(Connection connection,
            String idVendedor, int startIndex, int count)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, "
                    + "valor, peso, gastos_envio, fecha_pago, "
                    + "fecha_envio FROM pedido WHERE "
                    + "id_vendedor = ? AND fecha_envio IS NULL "
                    + "ORDER BY n_compra ASC";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            preparedStatement.setString(1, idVendedor);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findByShippingPendentAndseller

    public List<OrderDetailsVO> findAll(Connection connection, int startIndex,
            int count) throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT n_pedido, n_compra, "
                    + "id_vendedor, valor, peso, gastos_envio, "
                    + "fecha_pago, fecha_envio FROM pedido";

            preparedStatement = connection.prepareStatement(queryString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            // Read objects.
            List<OrderDetailsVO> orderVOs = new ArrayList<OrderDetailsVO>();
            int currentCount = 0;

            if ((startIndex >= 1) && resultSet.absolute(startIndex)) {
                do {
                    int i = 1;
                    Long nPedido = resultSet.getLong(i++);
                    Long nCompra = resultSet.getLong(i++);
                    String idVendedor = resultSet.getString(i++);
                    double valor = resultSet.getDouble(i++);
                    double peso = resultSet.getDouble(i++);
                    double gastosEnvio = resultSet.getDouble(i++);
                    Calendar fechaPago = null;
                    Timestamp fPago = resultSet.getTimestamp(i++);
                    if (fPago != null) {
                        fechaPago = Calendar.getInstance();
                        fechaPago.setTimeInMillis(fPago.getTime());
                    }
                    Calendar fechaEnvio = null;
                    Timestamp fEnvio = resultSet.getTimestamp(i++);
                    if (fEnvio != null) {
                        fechaEnvio = Calendar.getInstance();
                        fechaEnvio.setTimeInMillis(fEnvio.getTime());
                    }

                    orderVOs.add(new OrderDetailsVO(nPedido, nCompra,
                            idVendedor, valor, peso, gastosEnvio, fechaPago,
                            fechaEnvio));

                    currentCount++;

                } while (resultSet.next() && (currentCount < count));
            }// if

            // Return value objects.
            return orderVOs;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// findAll

    public void update(Connection connection, OrderDetailsVO orderVO)
            throws InstanceNotFoundException, InternalErrorException {
        PreparedStatement preparedStatement = null;

        try {

            // Create "preparedStatement".
            String queryString = "UPDATE pedido SET id_vendedor = ?, "
                    + "valor = ?, peso = ?, gastos_envio = ?, "
                    + "fecha_pago = ?, fecha_envio = ? WHERE "
                    + "n_pedido = ? AND n_compra = ?";

            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            int i = 1;
            preparedStatement.setString(i++, orderVO.getIdVendedor());
            preparedStatement.setDouble(i++, orderVO.getValor());
            preparedStatement.setDouble(i++, orderVO.getPeso());
            preparedStatement.setDouble(i++, orderVO.getGastosEnvio());
            if (orderVO.getFechaPago() != null) {
                preparedStatement.setTimestamp(i++, new Timestamp(orderVO.getFechaPago().getTime().getTime()));
            } else {
                preparedStatement.setTimestamp(i++, null);
            }
            if (orderVO.getFechaEnvio() != null) {
                preparedStatement.setTimestamp(i++, new Timestamp(orderVO.getFechaEnvio().getTime().getTime()));
            } else {
                preparedStatement.setTimestamp(i++, null);
            }
            preparedStatement.setLong(i++, orderVO.getnPedido());
            preparedStatement.setLong(i++, orderVO.getnCompra());

            // Execute query.
            int updatedRows = preparedStatement.executeUpdate();

            if (updatedRows == 0) {
                throw new InstanceNotFoundException(orderVO.getnPedido().longValue(), OrderDetailsVO.class.getName());
            }

            if (updatedRows > 1) {
                throw new SQLException("Fila duplicada para el n_pedido = '"
                        + orderVO.getnPedido().longValue() + "' y n_compra = '"
                        + orderVO.getnCompra().longValue()
                        + "' en la tabla 'Pedido'");
            }

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// update

    public void updateShippingDate(Connection connection, Long nOrder)
            throws InstanceNotFoundException, InternalErrorException {
        PreparedStatement preparedStatement = null;

        try {
            // Create "preparedStatement".
            String queryString = "UPDATE pedido SET "
                    + "fecha_envio = ? WHERE "
                    + "n_pedido = ?";

            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            int i = 1;
            preparedStatement.setTimestamp(i++,
                    new Timestamp(Calendar.getInstance().getTime().getTime()));
            preparedStatement.setLong(i++, nOrder);


            // Execute query.
            int updatedRows = preparedStatement.executeUpdate();

            if (updatedRows == 0) {
                throw new InstanceNotFoundException(nOrder.longValue(), OrderDetailsVO.class.getName());
            }

            if (updatedRows > 1) {
                throw new SQLException("Fila duplicada para el n_pedido = '"
                        + nOrder.longValue()
                        + "' en la tabla 'Pedido'");
            }

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// updateShippingDate

    public void updatePaymentDate(Connection connection, Long nOrder)
            throws InstanceNotFoundException, InternalErrorException {
        PreparedStatement preparedStatement = null;

        try {
            // Create "preparedStatement".
            String queryString = "UPDATE pedido SET "
                    + "fecha_pago = ? WHERE "
                    + "n_pedido = ?";

            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            int i = 1;
            preparedStatement.setTimestamp(i++,
                    new Timestamp(Calendar.getInstance().getTime().getTime()));
            preparedStatement.setLong(i++, nOrder);


            // Execute query.
            int updatedRows = preparedStatement.executeUpdate();

            if (updatedRows == 0) {
                throw new InstanceNotFoundException(nOrder.longValue(), OrderDetailsVO.class.getName());
            }

            if (updatedRows > 1) {
                throw new SQLException("Fila duplicada para el n_pedido = '"
                        + nOrder.longValue()
                        + "' en la tabla 'Pedido'");
            }

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// updatePaymentDate

    public void remove(Connection connection, Long nPedido, Long nCompra)
            throws InstanceNotFoundException, InternalErrorException {
        PreparedStatement preparedStatement = null;

        try {

            // Create "preparedStatement".
            String queryString = "DELETE FROM pedido "
                    + "WHERE n_pedido = ? AND n_compra = ?";

            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setLong(1, nPedido.longValue());
            preparedStatement.setLong(2, nCompra.longValue());

            // Execute query.
            int removedRows = preparedStatement.executeUpdate();

            if (removedRows == 0) {
                throw new InstanceNotFoundException(nPedido.longValue(),
                        OrderDetailsVO.class.getName());
            }

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }

    }// remove
}
